package org.lwz.space.service;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Liaowz on 2015/11/29.
 */
public class SocketDemo {
    public static void main(String[] args) throws Exception {

        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();

        InputStream inputStream = socket.getInputStream();

        byte[] bytes = new byte[1024];
        inputStream.read(bytes);

        ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
        BufferedReader bfReader = new BufferedReader(new InputStreamReader(arrayInputStream));
        OutputStream outputStream = socket.getOutputStream();
        while (true) {
            String line = bfReader.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
        }

        outputStream.write("Hello World".getBytes());

        bfReader.close();
        outputStream.close();
        socket.close();


    }
}
